#include <stdio.h>
#include <string.h>
#include "file.h"

int loadname(char *file, char *name, char *paswd)
{
	FILE *fp;
	char buffer[MAXROW] = {0};
	char *cpwd = NULL;
	int iRow=20;
	int Length;
	fp = fopen(file,"rw+");
	if(fp == NULL)
	{
		printf("open file error");
		return -1;
	}
	else
	{
		printf("open cfg file %s successfully\n",file);
	}
	while(fgets(buffer, MAXROW+1,fp) != NULL)
	{
		if(buffer[0]=='#')
		{
			continue;
		}
		if(strncmp(name , buffer , strlen(name)) == 0)
		{
			Length=strlen(name);
			if(buffer[strlen(name)]=='=')
			{
				Length += 1;
				do	
				{
					*(paswd++)=buffer[Length];
				}
				while(buffer[Length++]!='\0');
				fclose(fp);
				return 0;
			}
		}
		else
		{
			continue;
		}
	}
	fclose(fp);
	return -1;	
}

int writename(char *file,char *name ,char *password)
{
	FILE *fp;	
	fp = fopen(file,"a+");
	if(fp == NULL)
	{
		printf("open file error");
		return -1;
	}
	else
	{
		printf("open cfg file %s successfully\n",file);
	}
	fwrite(name,strlen(name),1,fp);
	fwrite("=",1,1,fp);
	fwrite(password,strlen(password),1,fp);	
	fclose(fp);
	return 0;
}
